package cn.com.lyj6851.modules.mp.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.mp.domain.YdyArticle;
import cn.com.lyj6851.modules.mp.service.YdyArticleService;
import cn.com.lyj6851.modules.mp.service.dto.YdyArticleDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyArticleQueryCriteria;
import cn.com.lyj6851.modules.mp.service.mapper.YdyArticleMapper;
import cn.com.lyj6851.modules.mp.vo.YdyArticleQueryVo;
import cn.com.lyj6851.utils.FileUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YdyArticleServiceImpl extends BaseServiceImpl<YdyArticleMapper, YdyArticle> implements YdyArticleService {
    @Resource
    private IGenerator generator;

    @Resource
    private YdyArticleMapper articleMapper;

    @Value("${file.path}")
    private String uploadDirStr;


    /**
     * 获取文章列表
     * @param page 页码
     * @param limit 条数
     * @return List
     */
    @Override
    public List<YdyArticleQueryVo> getList(int page, int limit) {
        Page<YdyArticle> pageModel = new Page<>(page,limit);
        IPage<YdyArticle> pageList = articleMapper.selectPage(pageModel, Wrappers.<YdyArticle>lambdaQuery()
                .orderByDesc(YdyArticle::getId));

        return generator.convert(pageList.getRecords(),YdyArticleQueryVo.class);
    }

    /**
     * 获取文章详情
     * @param id id
     * @return YdyArticleQueryVo
     */
    @Override
    public YdyArticleQueryVo getDetail(int id) {
        return generator.convert(getById(id),YdyArticleQueryVo.class);
    }

    @Override
    public void incVisitNum(int id) {
        articleMapper.incVisitNum(id);
    }

    @Override
    public Map<String, Object> queryAll(YdyArticleQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdyArticle> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YdyArticleDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<YdyArticle> queryAll(YdyArticleQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdyArticle.class, criteria));
    }

    @Override
    public void download(List<YdyArticleDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdyArticleDto ydyArticle : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类id", ydyArticle.getCid());
            map.put("文章标题", ydyArticle.getTitle());
            map.put("文章作者", ydyArticle.getAuthor());
            map.put("文章图片", ydyArticle.getImageInput());
            map.put("文章简介", ydyArticle.getSynopsis());
            map.put(" content",  ydyArticle.getContent());
            map.put("文章分享标题", ydyArticle.getShareTitle());
            map.put("文章分享简介", ydyArticle.getShareSynopsis());
            map.put("浏览次数", ydyArticle.getVisit());
            map.put("排序", ydyArticle.getSort());
            map.put("原文链接", ydyArticle.getUrl());
            map.put("状态", ydyArticle.getStatus());
            map.put("是否隐藏", ydyArticle.getHide());
            map.put("管理员id", ydyArticle.getAdminId());
            map.put("商户id", ydyArticle.getMerId());
            map.put("产品关联id", ydyArticle.getProductId());
            map.put("是否热门(小程序)", ydyArticle.getIsHot());
            map.put("是否轮播图(小程序)", ydyArticle.getIsBanner());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
